<?php
class Task {
	private $_contentSpecs;
	private $_dbConnection;
	private $_languageId;

	public function __construct($databaseName = null, $specs, $languageId = 25) {
		$this->_dbConnection = new Database($databaseName);
		$this->_languageId = $languageId;
		$this->_contentSpecs = json_decode(file_get_contents($specs), true);
	}

	public function validateGenre($type, $parentId = -1) {
		$result = array();
		$result['type'] = $type;
		$result['result'] = array();

		try {
			$genreSpec = $this->_contentSpecs[$type];
			$query = $genreSpec['query'];

			$pdoStmt = $this->_dbConnection->query($query, array(':language_id' => $this->_languageId, ':parent_id' => $parentId));

			foreach ($pdoStmt as $val) {
				$ret = $this->validate($val, $genreSpec);
				$g = array_keys($ret);
				$genreId = $g[0];

				$result['result'][$genreId] = $ret[$genreId];
			}
		}
		catch (Exception $e) {
			print_r($e);
		}
		catch (PDOException $pdoe) {
			print_r($pdoe);
		}

		return $result;
	}

	public function validateContent($type, $genreId) {
		$result = array();

		$byGenreSpec = $this->_contentSpecs[$type];
		$query = $byGenreSpec['query'];

		try {
			//TODO val for posters ...
			$pdoStmt = $this->_dbConnection->query($query, array($genreId, $this->_languageId));
			$result['type'] = $type;

			foreach ($pdoStmt as $val) {
				$tmp = $this->validate($val, $byGenreSpec);
				$result['result'][] = $tmp;
			}
		}
		catch (Exception $e) {
			print_r($e);
		}
		catch (PDOException $pdoe) {
			print_r($pdoe);
		}

		return $result;
	}

	private function validate($data, $specs) {
		$spec = array();
		$result = array();

		$cms_id = 'psuedo';
		
		if (isset($data['cms_id'])) {
			$cms_id = $data['cms_id'];
		}
		else if (isset($data['_id'])) {
			$cms_id = $data['_id'];
		}

		foreach ($data as $key => $val) {
			$spec = array();
			if (isset($specs[$key]) && $specs[$key]['require']) {
				$spec = $specs[$key];

				$spec['status'] = 'KO';
				$val = trim($val);
				$spec['value'] = $val;
				$spec['message'] = '';

				/*
				if (is_array($spec) && isset($spec['dimension']) && $spec['dimension']['require']) {
					//TODO
				}
				 */
				$type = $spec['type'];

				$status = array();
				switch($type) {
					case 'string' : $this->textHandler(&$spec, $val); break;
					case 'integer' : $this->numericHandler(&$spec, $val); break;
					case 'jpg' :
					case 'png' :
					case 'gif' : $status = $this->mediaHandler(&$spec, $val); break;
					default: break;
				}
			}

			if (isset($spec['status']) && isset($specs[$key])) {

				//unset($spec['dimension']);
				$result[$cms_id][$key] = $spec;
			}
		}

		return $result;
	}

	private function textHandler(&$spec, $val) {
		$size = strlen($val);

		$max = isset($spec['count']) ? $spec['count'] : 1000;

		if (0 == $size) {
			$spec['message'] = 'This field should not be empty!';
		}
		else if ($size > $max) {
			$over = -($max - $size);
			$spec['message'] = "Character count should not longer than [{$max}] character(s).  [{$over}] character(s) over limit.";
		}
		else {
			$spec['status'] = 'OK';
		}
	}

	private function mediaHandler(&$spec, $val) {
		$spec['message'] = 'Need to check image size and type';
		$spec['value'] = $val;
		$spec['status'] = 'KO';
	}

	private function numericHandler(&$spec, $val) {
		$spec['message'] = 'Value need to be numeric';
		$spec['value'] = $val;
		$spec['status'] = 'KO';

		if (is_numeric($val)) {
			$spec['status'] = 'OK';
			$spec['message'] = '';
		}
	}
}

?>
